# -*- coding: utf-8 -*-
"""
Created on Fri Sep 20 16:59:13 2024

@author: Administrator
"""
class Person:
    #class variable
    # class variable is shared by all instances
    counter=0
    
    #constructor
    def __init__(self,name,age):
        # use class variable
        Person.counter += 1
        # use instance variable
        self.name = name
        self.age = age
    
    # instance method    
    def greet(self):
        return f"hi ,it is {self.name}"
    
    # class method ，the first parameter is cls
    # it can access class variable
    @classmethod
    def create_anonymous(cls):
        return Person("anonymous", 30)
    
    # static method
    # it can not access class variable and instance variable
    @staticmethod
    def add(a,b):
        return a+b
    
    
p1 = Person("bob", 21)
p2 = Person.create_anonymous()

print(f"class variable: Person.counter={Person.counter} ")

print(f"class variable: p1.counter={p1.counter} ")

print(f"class variable Person.counter={Person.counter} ")

print(f"instance variable: {p1.greet()}")

print(f"instance variable:{p2.greet()}")

a=1
b=2 
c=Person.add(a,b)

print(f"static method: Person.add({a},{b})={c}")